package com.znsb.msfq.app;

import android.app.Activity;
import android.app.Application;
import android.app.Dialog;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mobstat.SendStrategyEnum;
import com.baidu.mobstat.StatService;
import com.znsb.msfq.R;
import com.znsb.msfq.utils.OverallData;

import cn.jpush.android.api.JPushInterface;


/**
 * @name：
 * @author： 杨 广
 * @phone： 17382373271
 * @createTime： 2017/2/8
 * @modifyTime： 2017/2/8
 * @explain：
 */

public class ZnsbApp extends Application {
    /**
     * application 单例
     */
    private static ZnsbApp singleton = null;
    public static Context mContext;

    public synchronized static ZnsbApp getInstance() {
        if (null == singleton) {
            singleton = new ZnsbApp();
        }
        return singleton;
    }

    @Override
    public void onCreate() {
        mContext = getApplicationContext();
        /**
         * 记录崩溃日志并处理崩溃的异常
         */
//        CrashHandler crashHandler = CrashHandler.getInstance();
//        crashHandler.init(getApplicationContext());

        //极光推送
        JPushInterface.setDebugMode(false);    // 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);            // 初始化 JPush

        // 百度统计
        //设置日志发送策略
        StatService.setSendLogStrategy(this, SendStrategyEnum.APP_START,
                1, false);
        StatService.setOn(this, StatService.EXCEPTION_LOG);
        //设置日志发送延迟时间
        StatService.setLogSenderDelayed(10);
        // 一次启动间隔时间设定
        StatService.setSessionTimeOut(30);
        StatService.setDebugOn(false);

        /**
         * 获取渠道名
         * @param context 此处习惯性的设置为activity，实际上context就可以
         * @return 如果没有获取成功，那么返回值为空
         */
        String channelName = "HW";//设置默认的渠道名
        try {
            PackageManager packageManager = this.getPackageManager();
            if (packageManager != null) {
                //注意此处为ApplicationInfo 而不是 ActivityInfo,因为友盟设置的meta-data是在application标签中，而不是某activity标签中，所以用ApplicationInfo
                ApplicationInfo applicationInfo = packageManager.
                        getApplicationInfo(this.getPackageName(), PackageManager.GET_META_DATA);
                if (applicationInfo != null) {
                    if (applicationInfo.metaData != null) {
                        channelName = String.valueOf(applicationInfo.metaData.get("BaiduMobAd_CHANNEL"));

                    }
                }

            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        //得到打包的渠道名  channelName
        StatService.setAppChannel(this, channelName, true);
        OverallData.setChannel(channelName);
    }
//
//    public void createLoadingDialog(String msg) {
//        Dialog dialog = new Dialog(Activity, R.style.loading);
//        View view = LayoutInflater.from(ZnsbApp.mContext).inflate(R.layout.loading, null);
//        dialog.getWindow().getAttributes().gravity = Gravity.CENTER;
//        TextView content = (TextView) view
//                .findViewById(R.id.loading_tv);
//        content.setText(msg);
//        ImageView loading = (ImageView) view
//                .findViewById(R.id.loading_img);
//        AnimationDrawable animationDrawable = (AnimationDrawable) loading.getDrawable();
//        animationDrawable.start();
//        dialog.setContentView(view);
//        dialog.show();
//    }


}
